/*
 * Cubem - Copyright (C) 2008 Ewan Davies
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 *
 */
package cubem;

import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.*;

/**
 * @author Ewan
 */
public abstract class TimerCanvas extends Canvas {

    public TimerCanvas() {
        /**
         * Prepare the timer
         */
        myTimer = new Timer();
        timerThread = null;
        timeStarted = timeStopped = 0;
    }
    
    /**
     * Application states
     */
    public static final int STATUS_NOT_STARTED = 0;
    public static final int STATUS_RUNNING = 1;
    public static final int STATUS_STOPPED = 2;
   
   /**
     * Colours used
     */
    protected static final int COLOUR_WHITE = 0x00FFFFFF;
    protected static final int COLOUR_BLACK = 0x00000000;
    protected static final int COLOUR_RED = 0x00FF0000;
    protected static final int COLOUR_BLUE = 0x000000FF;
    protected static final int COLOUR_GREEN = 0x0000FF00;
    protected static final int COLOUR_BACKGROUND = COLOUR_BLACK;
    protected static final int COLOUR_FOREGROUND = COLOUR_WHITE;
    protected static final int COLOUR_BEST_TIME = COLOUR_GREEN;
    protected static final int COLOUR_WORST_TIME = COLOUR_RED;

    protected CubemMIDlet parent;
    protected Timer myTimer;
    protected TimerTask timerThread;
    protected long timeStarted;
    protected long timeStopped;
    protected int status;
        
    /**
     * Sets the parent of the TimerCanvas
     * Must be called immediately after construction
     */
    public void setParent(CubemMIDlet owner) {
        parent = owner;
    }
    
    /**
     * Gets the status of the timer
     * @return timer status
     */
    public int getStatus(){
        return status;
    }
    
    /**
     * Starts the timer
     */
    public void startTimer() {
        timeStarted = System.currentTimeMillis();
        status = STATUS_RUNNING;
        timerThread = new UpdateDisplayTask();
        myTimer.schedule(timerThread, 10, 100); //every 0.1 sec
    }
    
    /**
     * Stops the timer
     */
    public void stopTimer() {
        timeStopped = System.currentTimeMillis();
        status = STATUS_STOPPED;
        timerThread.cancel();
        timerThread = null;
        repaint();
    }
    
     /**
     * Resets the timer
     */
    public void reset() {
        if (timerThread != null) {
            timerThread.cancel();
        }
        status = STATUS_NOT_STARTED;
    }
    
    /**
     * A class representing the task which triggers a repaint of the canvas
     */
    class UpdateDisplayTask extends TimerTask {

        public void run() {
            repaint();
        }
    }

}